{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[655, 129, 106, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "# --------------------------------------------------------------------------- #\n",
    "# import the modbus libraries we need\n",
    "# --------------------------------------------------------------------------- #\n",
    "from pymodbus.version import version\n",
    "from pymodbus.server.asynchronous import StartTcpServer\n",
    "from pymodbus.device import ModbusDeviceIdentification\n",
    "from pymodbus.datastore import ModbusSequentialDataBlock\n",
    "from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext\n",
    "from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer\n",
    "from IPython.display import clear_output\n",
    "\n",
    "# --------------------------------------------------------------------------- #\n",
    "# import the twisted libraries we need\n",
    "# --------------------------------------------------------------------------- #\n",
    "from twisted.internet.task import LoopingCall\n",
    "\n",
    "# --------------------------------------------------------------------------- #\n",
    "# configure the service logging\n",
    "# --------------------------------------------------------------------------- #\n",
    "import logging\n",
    "logging.basicConfig()\n",
    "log = logging.getLogger()\n",
    "log.setLevel(logging.ERROR)\n",
    "\n",
    "# --------------------------------------------------------------------------- #\n",
    "# define your callback process\n",
    "# --------------------------------------------------------------------------- #\n",
    "\n",
    "\n",
    "def printing_context(a):\n",
    "    clear_output(wait=True)\n",
    "    context = a[0]\n",
    "    register = 3\n",
    "    slave_id = 0x01\n",
    "    address = 0x0\n",
    "    values = context[slave_id].getValues(register, address, count=5)\n",
    "    print(values)\n",
    "    \n",
    "\n",
    "\n",
    "def run_updating_server():\n",
    "    # ----------------------------------------------------------------------- # \n",
    "    # initialize your data store\n",
    "    # ----------------------------------------------------------------------- # \n",
    "    \n",
    "    store = {0x01 : ModbusSlaveContext(\n",
    "        di=ModbusSequentialDataBlock(0, [0]*100),\n",
    "        co=ModbusSequentialDataBlock(0, [0]*100),\n",
    "        hr=ModbusSequentialDataBlock(0, [0]*100),\n",
    "        ir=ModbusSequentialDataBlock(0, [0]*100))}\n",
    "    \n",
    "\n",
    "    context = ModbusServerContext(slaves=store, single=False)\n",
    "    \n",
    "    # ----------------------------------------------------------------------- # \n",
    "    # initialize the server information\n",
    "    # ----------------------------------------------------------------------- # \n",
    "    identity = ModbusDeviceIdentification()\n",
    "    identity.VendorName = 'pymodbus'\n",
    "    identity.ProductCode = 'PM'\n",
    "    identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'\n",
    "    identity.ProductName = 'pymodbus Server'\n",
    "    identity.ModelName = 'pymodbus Server'\n",
    "    identity.MajorMinorRevision = version.short()\n",
    "    \n",
    "    # ----------------------------------------------------------------------- # \n",
    "    # run the server you want\n",
    "    # ----------------------------------------------------------------------- # \n",
    "    time = 1  # 5 seconds delay\n",
    "    loop = LoopingCall(f=printing_context, a=(context,))\n",
    "    loop.start(time, now=False) # initially delay by time\n",
    "    StartTcpServer(context, identity=identity, address=(\"10.75.15.61\", 5020)) # to use the 502 port you need to run this script as root\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    run_updating_server()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
  },
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
